const fetch = require("nb-fetch/fetch")

exports.getLabel = async ctx => {
  var {key, sign} = makeSign()
  var url = process.env.GET_LABEL_URL
  if (!url) {
    return ctx.body = {
      bool: false,
      msg: '未配置选签地址'
    }
  }
  try {
    var res = await fetch.get(url, {
      params: {
        key,
        sign
      }
    })
  } catch (err) {
    console.log(err)
    return ctx.body = {
      bool: false,
      msg: '请求出错了'
    }
  }
  var data = res.data
  if (data.errcode === 1) {
    return ctx.body = {
      bool: false,
      msg: '签名错误'
    }
  }
  return ctx.body = {
    bool: true,
    msg: '',
    result: data
  }
}

exports.setLabel = async ctx => {
  var label = ctx.request.body.label
  if (!label) {
    return ctx.body = {
      bool: false,
      msg: '你必须提供一个标签'
    }
  }
  log(label)
  var url = process.env.SET_LABEL_URL
  if (!url) {
    return ctx.body = {
      bool: false,
      msg: '未设置地址'
    }
  }
  var {key, sign} = makeSign()
  try {
    var res = await fetch.get(url, {
      params: {
        key,
        sign,
        value: label
      }
    })
  } catch (err) {
    return ctx.body = {
      bool: false,
      msg: '设置选签失败',
      err
    }
  }
  return ctx.body = {
    bool: true,
    msg: '设置成功',
    result: res.data.result
  }
}
